package com.neoCalf.neoUser.controller;

import com.neoCalf.neoCommon.entity.UserInfo;
import com.neoCalf.neoCommon.model.R;
import com.neoCalf.neoCommon.model.dto.UserInfoDto;
import com.neoCalf.neoUser.service.UserInfoService;
import com.sun.istack.internal.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/6 22:43
 */
@RestController
@RequestMapping("/info")
@Validated
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @GetMapping("/findUserInfo")
    public R<UserInfoDto> findUserInfo(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        UserInfoDto detailUserInfo = userInfoService.findDetailUserInfo(userId);
        return R.ok(detailUserInfo);
    }

    @PostMapping("/findUserInfo4")
    public R<UserInfoDto> findUserInfo4(@RequestParam("userId") @NotEmpty String userId) {
        UserInfoDto detailUserInfo = userInfoService.findDetailUserInfo(userId);
        return R.ok(detailUserInfo);
    }

    @PostMapping("updateUserInfo")
    public R<String> updateUserInfo(HttpServletRequest request, @RequestBody @NotNull UserInfo userInfo) {
        String userId = request.getHeader("userId");
        userInfoService.updateUserInfo(userId, userInfo);
        return R.ok();
    }

    @PutMapping("updatePassword")
    public R<String> updatePassword(HttpServletRequest request,
                                    @NotEmpty String oldPassword,
                                    @NotEmpty String password) {
        userInfoService.updatePassword(request.getHeader("userId"), oldPassword, password);
        return R.ok();
    }

    @PostMapping("saveAvatar")
    public R<String> saveAvatar(HttpServletRequest request, @RequestParam("url") @NotEmpty String url) {
        String userId = request.getHeader("userId");
        userInfoService.saveAvatar(userId, url);
        return R.ok();
    }
}
